#include"head.h"


//双向循环链表的初始化
Dlistp list_init()
{
	struct list_head *head;
	head = calloc(1,sizeof(struct list_head));
	if(head == NULL)
	{
		perror("head init failed");
	}
	head->prev = head;
	head->next = head;
return head;
}

//添加一个新的节点
void list__add(Dlistp new,Dlistp prev,Dlistp next)
{
	next->prev = new;
	new->next = next;
	new->prev = prev;
	prev->next = new;
}

//在tail部添加一个新节点
void list_add_tail(Dlistp new,Dlistp head)
{
	//将新节点、链表为节点、头结点作为参数
	list__add(new,head->prev,head);	
}

void list__del(Dlistp prev,Dlistp next)
{
	next->prev = prev;
	prev->next = next;
}

void list_del(Dlistp entry)
{
	//将前节点的前面的接节点和后面的节点连接起来
	list__del(entry->prev,entry->next);
	free(entry);
}

//遍历链表查找一个节点
Dlistp fine_node(struct in_addr* sin_addr,Dlistp head)
{
	Dlistp temp = head;
	while(temp->next!=head) //遍历链表，如果找到所需节点则立即返回
	{	
		temp = temp->next;
		if(temp->sin_addr.s_addr == sin_addr->s_addr)
		{	
			return temp;	
		}
	}
	
return head;     //如果没有找都所需节点，则将头节点返回
}

//遍历链表并打印
void ergodic_list(Dlistp head)
{
	Dlistp temp = head;
	while(temp->next != head)
	{
		temp = temp->next;
		printf("[ip:%s port:%hu]\n",inet_ntoa(temp->sin_addr),ntohs(temp->sin_port));
	}
}

//遍历链表，返回节点个数
int count_numlist(Dlistp head)
{
	int num=0;	
	Dlistp temp = head;
	while(temp->next !=head)
	{
		temp = temp->next;
		num++;
	}
	return num;
}











